﻿using System.Linq;
using System.Windows;
using TheGameClient.Webservices;

namespace TheGameClient
{
	public partial class DlgLoadConnection
	{
		public SavedConnection SelectedConnection { get; private set; }
		public DlgLoadConnection()
		{
			InitializeComponent();
			App.Client.Connect_GetConnectionsCompleted += ClientConnectGetConnectionsCompleted;
			App.Client.Connect_GetConnectionsAsync(App.InternalSessionId);
		}

		void ClientConnectGetConnectionsCompleted(object sender, Connect_GetConnectionsCompletedEventArgs e)
		{
			App.Client.Connect_GetConnectionsCompleted -= ClientConnectGetConnectionsCompleted;
			if (e.Error != null) { App.ServerError(); return; }
			cmbExistingConnections.ItemsSource = e.Result.OrderBy(it => it.Name).ToList();
		}

		private void OkButtonClick(object sender, RoutedEventArgs e)
		{
			if (SelectedConnection != null)
			{
				DialogResult = true;
			}
		}

		private void CmbExistingConnectionsSelectionChanged(object sender, RoutedEventArgs e)
		{
			SelectedConnection = cmbExistingConnections.SelectedItem as SavedConnection;
		}

		private void CancelButtonClick(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}
	}
}

